package com.xinxin.base.util.legacy;

import java.math.BigDecimal;
import java.math.RoundingMode;

public final class BigDecimalUtil {

	public static String formatNumber(BigDecimal number, int scale){
		if (number == null  || number.compareTo(BigDecimal.ZERO) <= 0){
			return "0";
		}

		if (new BigDecimal(number.intValue()).compareTo(number)==0){
			return number.setScale(0).toPlainString();
		}else {
			return number.setScale(scale, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
		}
	}

	public static String formatNumber(BigDecimal number){
		return formatNumber(number, 3);
	}

	public static void main(String[] args) {
		System.out.println(formatNumber(new BigDecimal("1111111111111111.0300")));
		System.out.println(formatNumber(new BigDecimal("1")));
		System.out.println(formatNumber(new BigDecimal("90.000")));
		System.out.println(formatNumber(new BigDecimal("1.00100"), 5));
	}
}
